package com.heatup.apt.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.common.util.ResultBean;
import com.heatup.apt.management.service.IndexResourceService;
import com.heatup.apt.management.service.coach.CoachService;
import com.heatup.apt.model.IndexResource;
import com.heatup.apt.model.coach.CoachInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * API 首页资源控制
 *
 * IndexResource 控制层
 *
 */
@Controller
@RequestMapping("system/indexResource")
public class IndexResourceController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(IndexResourceController.class);

    @Resource
    private IndexResourceService indexResourceService;
    @Resource
    private CoachService coachService;


    /**
     * 首页资源列表
     *
     * @author blank 2017/7/13 上午9:59
     */
    @RequestMapping("list")
    @ResponseBody
    public ModelAndView list(){

        ModelAndView modelAndView = new ModelAndView("operative/new_index_list");
        return modelAndView;


    }



    /**
     * 首页资源新增
     *
     * @param id 主键
     * @author blank 2017/7/13 上午9:59
     */
    @RequestMapping("add")
    @ResponseBody
    public ModelAndView add(Integer id){

        ModelAndView modelAndView = new ModelAndView("operative/new_index");
        if (id != null) {
            modelAndView.addObject("data", indexResourceService.selectById(id));
        }
        List<CoachInfo> coachInfoList = coachService.queryCoachIdAndNickName();
        modelAndView.addObject("coachList", JSONObject.toJSONString(coachInfoList));
        return modelAndView;
    }


    /**
     * 新资源分页
     *
     * @param indexResourceQuery 新资源查询参数对象
     * @author blank 2017/7/13 上午10:05
     */
    @RequestMapping("page")
    public void page(IndexResource indexResourceQuery, HttpServletResponse response) {


        ResultBean resultBean = new ResultBean();
        Map<String, Object> resultMap = new HashMap<>();

        try {
            Map<String, List<IndexResource>> listMap = indexResourceService.queryIndexResourceList(indexResourceQuery);

            resultMap.put("list", listMap);
            resultBean.setData(resultMap);
            resultBean.setCode(ResultBean.CODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询首页资源出错 " + e.toString() + e.getCause(), e);
            resultBean.setCode(ResultBean.CODE_FAIL);
            resultBean.setMsg("查询首页资源出错 " + e.toString() + e.getCause());
        }
        responseSendMsg(response, JsonUtils.toJsonStrWithDateFormat(resultBean));
    }

    /**
     * 新资源保存
     *
     * @param indexResource 保存对象
     * @author blank 2017/7/13 下午12:23
     */
    @RequestMapping("save")
    public void save(HttpServletResponse response, IndexResource indexResource) {
        ResultBean resultBean = new ResultBean();

        if (indexResource != null) {
            try {
                boolean s = indexResourceService.save(indexResource);
                if (s) {
                    resultBean.setMsg("保存成功");
                    resultBean.setCode(ResultBean.CODE_SUCCESS);

                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("首页资源保存出错 " + e.toString() + e.getCause(), e);
                resultBean.setCode(ResultBean.CODE_FAIL);
                resultBean.setMsg("首页资源保存出错 " + e.toString() + e.getCause());
            }
            responseSendMsg(response, JsonUtils.toJsonStrWithDateFormat(resultBean));


        }


    }

    /**
     * 新资源删除
     *
     */
    @RequestMapping("del")
    @ResponseBody
    public String coachDelete(@RequestParam Integer id) {
        String result = "";
        if (id == null) {
            result = "删除失败";
            return result;
        }
        boolean success = indexResourceService.deleteById(id);
        if (success) {
            result = "删除成功";
        }

        return result;
    }

    /**
     * 新资源排序
     *
     * @author blank 2017/8/3 下午4:46
     */
    @RequestMapping("sort")
    public void sort(HttpServletResponse response, String sortString){

        try {

            Integer count = indexResourceService.updateSort(sortString);
            responseSendMsg(response, count + "");
        } catch (Exception e) {
            e.printStackTrace();
            responseSendMsg(response, "排序失败!");
        }
    }

}